﻿// Write a method that adds two polynomials. 
// Represent them as arrays of their coefficients as in the example below:
// x^2 + 5 = 1x^2 + 0x + 5 -> 501

using System;
using System.Globalization;

public class AddPolynomialsProgram
{
    static void Main()
    {
        decimal[] polynomial1 = ReadPolynomial();
        Console.Write("You have enter a first polynomial: ");
        PrintPolynomial(polynomial1);

        decimal[] polynomial2 = ReadPolynomial();
        Console.Write("You have enter a second polynomial: ");
        PrintPolynomial(polynomial2);

        decimal[] polynomialSum = AddPolynomials(polynomial1, polynomial2);
        Console.Write("The Sum of the polynomials is: ");
        PrintPolynomial(polynomialSum);
    }

    public static decimal[] ReadPolynomial()
    {
        int degree;
        do
        {
            Console.Write("Enter the degree of the polynomial: ");
        } while (!int.TryParse(Console.ReadLine(), out degree) || degree < 0);
        decimal[] polynomial = new decimal[degree + 1];
        for (int i = polynomial.Length - 1; i >= 0; i--)
        {
            do
            {
                Console.Write("Enter x^{0} coefficient: ", i);
            } while (!decimal.TryParse(Console.ReadLine().Replace(",", "."), NumberStyles.Number, CultureInfo.InvariantCulture, out polynomial[i]));

        }

        return polynomial;
    }

    public static decimal[] AddPolynomials(decimal[] firstPol, decimal[] secondPol)
    {
        int maxLength = Math.Max(firstPol.Length, secondPol.Length);
        decimal[] result = new decimal[maxLength];
        for (int i = firstPol.Length - 1; i >= 0; i--)
        {
            if (i < firstPol.Length && i < secondPol.Length)
            {
                result[i] = firstPol[i] + secondPol[i];
            }
            else if (i < firstPol.Length)
            {
                result[i] = firstPol[i];
            }
            else
            {
                result[i] = secondPol[i];
            }
        }

        return result;
    }

    public static void PrintPolynomial(decimal[] polynomialSum)
    {
        int countZero = 0;
        for (int i = polynomialSum.Length - 1; i >= 0; i--)
        {
            if (polynomialSum[i] != 0)
            {
                decimal value = Math.Abs(polynomialSum[i]);
                if (i == 0)
                {
                    Console.Write("{0}", value);
                }
                else if (i == 1)
                {
                    Console.Write("x.{0}", value);
                }
                else
                {
                    Console.Write("x^{0}.{1}", i, value);
                };
            }
            else
            {
                countZero++;
            }
            // add sign +/-
            if (i > 0)
            {
                if (polynomialSum[i - 1] != 0)
                {
                    if (polynomialSum[i - 1] > 0)
                    {
                        Console.Write(" + ");
                    }
                    else
                    {
                        Console.Write(" - ");
                    }
                }
            }
        }
        // if all element is zero print zero
        if (countZero == polynomialSum.Length)
        {
            Console.Write("0");
        }
        Console.WriteLine();
    }
}